<style>
    .dn {
        display: none;
    }

    .attachments {
        display: block;
        float: left;
        overflow: hidden;
        text-align: center;
    }

    .option-item {
        overflow: hidden;
        margin-top: 5px;
        color: #1E9FFF;
        text-align: center;
    }

    .preview {
        cursor: pointer;
    }

    .file-a {
        font-size: 12px;
        word-break: break-all;
        text-overflow: ellipsis;
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        margin-top: 5px;
    }

    .cardU {
        background-image: url(../../assets/images/card_u.png);
        background-repeat: no-repeat;
        background-position: center;
        width: calc(100% - 152px);
        height: 150px;
        padding: 0;
    }

    .cardU img {
        width: 100%;
        height: 100%;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">编号：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="feeItemCode" class="layui-input" type="text" placeholder="请输入编号" />
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"
                                        lay-submit layui-form-keyDownSearch="true">查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置
                                    </button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="warehouseBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="warehouseTable" lay-filter="warehouseTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="warehouseTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    {{# if(d.status == '1'){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="updateStatus">禁用</a>
    {{# } }}
    {{# if(d.status == '0'){ }}
    <a class="layui-btn layui-btn-xs" lay-event="updateStatus">启用</a>
    {{# } }}
  </script>

<!-- 表单弹窗 -->
<script type="text/html" id="warehouseForm">
    <form lay-filter="warehouseForm" class="layui-form model-form">
      <input name="id" type="hidden"/>
  
      <div class="layui-form-item">
        <div class="layui-col-md12">
          <label class="layui-form-label">ICON照片</label>
          <div class="layui-upload-drag cardU" id="photoUrlDiv">
            <!--<i class="layui-icon"></i>
            <p>点击上传，或将文件拖拽到此处</p>-->
            <input type="hidden" name="photoUrl" lay-verify="" data-name="ICON照片">
          </div>
        </div>
      </div>
  
      <div class="layui-form-item">
        <div class="layui-col-md12">
          <label class="layui-form-label">业务类型</label>
          <div class="layui-input-block">
            <select id="businessType" name="businessType" lay-filter="businessType" lay-search lay-verify="required">
                <option value="">请选择</option>
                <option value="1">托管业务</option>
                <option value="2">加工业务</option>
            </select>
          </div>
        </div>
      </div>
  
      <div class="layui-form-item" style="width: 100%;">
        <div class="layui-col-md12">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
              <select id="status" name="status" lay-filter="status" lay-search lay-verify="required">
                  <option value="">请选择</option>
                  <option value="1">启用</option>
                  <option value="2">禁用</option>
              </select>
          </div>
        </div>
      </div>
  
      <div class="layui-form-item">
        <div class="layui-col-md12">
          <label class="layui-form-label">收费项编码</label>
          <div class="layui-input-block">
            <input name="feeItemCode" placeholder="请输入编码" type="text" class="layui-input"
                   lay-verify="required|isOnlySn" required/>
          </div>
        </div>
       
      </div>
      <div class="layui-form-item">
        <div class="layui-col-md12">
            <label class="layui-form-label">收费项名称</label>
            <div class="layui-input-block">
              <input name="feeItemName" placeholder="请输入名称" type="text" class="layui-input"
                     lay-verify="required|isOnlyName" required/>
            </div>
          </div>
    
      </div>
        <div class="layui-form-item">
            <div class="layui-col-md12">
                <label class="layui-form-label">计价单位</label>
                <div class="layui-input-block">
                    <input name="pricingUnit" placeholder="请输入计价单位" type="text" class="layui-input" lay-verify=""
                           required/>
                </div>
            </div>
        </div>
      <div class="layui-form-item">
        <div class="layui-col-md12">
          <label class="layui-form-label">价格</label>
          <div class="layui-input-block">
            <input name="price" placeholder="请输入价格" type="text" class="layui-input" lay-verify=""
                   required/>
          </div>
        </div>
      </div>
      <div class="layui-form-item">
        <div class="layui-col-md12">
          <label class="layui-form-label">序号</label>
          <div class="layui-input-block">
            <input name="sortOrder" placeholder="请输入序号" type="text" class="layui-input"
                   lay-verify="sortOrder" required/>
          </div>
        </div>
      </div>
      <div class="layui-form-item">
        <div class="layui-col-md12">
          <label class="layui-form-label">备注</label>
          <div class="layui-input-block">
            <input name="remark" placeholder="请输入备注" type="text" class="layui-input"
                   lay-verify="remark" required/>
          </div>
        </div>
      </div>
  
      <div class="layui-form-item text-right">
        <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        <button class="layui-btn" lay-filter="warehouseFormSubmit" lay-submit>保存</button>
      </div>
    </form>
  </script>

<!-- js部分 -->
<script>

    var $, config;

    function stopBubbling(e) {
        e = window.event || e;
        if (e.stopPropagation) {
            e.stopPropagation();      //阻止事件 冒泡传播
        } else {
            e.cancelBubble = true;   //ie兼容
        }
    }

    //下载文件
    function downloadFile(realName, url) {
        stopBubbling(this);
        var $downForm = $("<form method='get'></form>");
        $downForm.attr("action", config.base_server + "api-file/downloadFile");
        var $input = $("<input type='hidden'>");
        $input.attr("name", "url");
        $input.val(url);
        $downForm.append($input);
        var $input1 = $("<input type='hidden'>");
        $input1.attr("name", "filename");
        $input1.val(realName);
        $downForm.append($input1);
        var $input2 = $("<input type='hidden'>");
        $input2.attr("name", "access_token");
        $input2.val(config.getToken().access_token);
        $downForm.append($input2);
        $(document.body).append($downForm);
        //提交表单，实现下载
        $downForm.submit();
    }

    //预览
    function previewImg(index) {
        stopBubbling(this);

        var obj = $(index).parent().prev().prev();
        var img = new Image();
        img.src = obj.attr("src");
        var height = img.height; //获取图片高度
        var width = img.width; //获取图片宽度
        if (width > 1000) {
            height = 1000 / width * height;
            width = 1000;
            if (height > 800) {
                width = 800 / height * width;
                height = 800;
            }
        }
        if (height > 800) {
            width = 800 / height * width;
            height = 800;
        }

        var imgHtml = "<img src='" + obj.attr("src") + "' style='width: " + width + "px; height: " + height + "px;' />";
        //弹出层
        layer.open({
            type: 1,
            shade: 0.8,
            offset: 'auto',
            area: [width + 'px', (height + 50) + 'px'],
            shadeClose: true,//点击外围关闭弹窗
            scrollbar: false,//不现实滚动条
            title: "图片预览", //不显示标题
            content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            cancel: function () {
                //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
            }
        });
    }

    layui.use(['layer', 'form', 'table', 'admin', 'laytpl', 'upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laytpl = layui.laytpl;

        // 渲染表格
        var insTb = table.render({
            elem: '#warehouseTable',
            url: config.base + 'warehouse/warehouse/feeItems/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                { type: "numbers", title: "序号", width: 70 }
                , { field: 'feeItemName', align: 'center', title: '名称', width: 150 }
                , { field: 'feeItemCode', align: 'center', title: '编码', width: 150 }
                // ,{field: 'companyId', align: 'center', title: '所属公司'}
                // ,{field: 'departmentName', align: 'center', title: '责任部门'}
                // ,{field: 'managerName', align: 'center', title: '责任人'}

                , { field: 'price', align: 'center', title: '价格' }
                , {
                    field: 'businessType',
                    align: 'center',
                    title: '业务类型',
                    templet: function (d) {
                        return d.businessType == 1 ? "托管业务" : "加工业务";
                    }
                }
                , {
                    field: 'remark', align: 'center', title: '备注',
                    templet: function (d) {
                        return d.remark ? d.remark : "无";
                    }
                }
                , {
                    field: 'status', align: 'center', title: '状态',
                    templet: function (d) {
                        return d.status == 1 ? '启用' : "禁用";
                    }
                }
                , { field: 'sortOrder', align: 'center', title: '排序' }
                , {
                    field: 'creatorName', align: 'center', title: '创建人', width: 140, templet: function (d) {
                        return d.creatorName ? d.creatorName : "未记录";
                    }
                }
                , { field: 'createTime', align: 'center', title: '创建时间', width: 180 }
                , { align: 'center', toolbar: '#warehouseTableBar', title: '操作', width: 150, fixed: 'right' }
            ]]
        });

        // 工具条点击事件
        table.on('tool(warehouseTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'updateStatus') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(warehouseTable)', function (obj) {
            table.reload('warehouseTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // form.verify({
        //     isOnlySn: function (value) {
        //         var isOnly = true;
        //         var idVal = $("form input[name='id']").val();
        //         admin.req('warehouse/warehouse/admin/administration/isOnly', { code: value, id: idVal }, function (res) {
        //             if (res.code == 200) {
        //                 isOnly = res.data;
        //             }
        //         }, 'GET', false);
        //         if (!isOnly) {
        //             return "仓库编码必须唯一"
        //         }
        //     },
        //     isOnlyName: function (value) {
        //         var isOnly = true;
        //         var idVal = $("form input[name='id']").val();
        //         var nameVal = $("form input[name='name']").val();
        //         admin.req('warehouse/warehouse/admin/administration/isOnly', { name: nameVal, id: idVal }, function (res) {
        //             if (res.code == 200) {
        //                 isOnly = res.data;
        //             }
        //         }, 'GET', false);
        //         if (!isOnly) {
        //             return "仓库名称必须唯一"
        //         }
        //     }
        // });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({ where: data.field, page: { curr: 1 } }, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({ where: field, page: { curr: 1 } }, 'data');
        });

        // 添加按钮点击事件
        $('#warehouseBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {

            console.info(data, 9090);

            admin.open({
                type: 1,
                area: '560px',
                offset: '120px',
                title: data ? '修改仓库' : '添加仓库',
                content: $('#warehouseForm').html(),
                success: function () {

                    upload.render({
                        elem: '#photoUrlDiv'
                        , url: config.upload_url + 'upload?path=' + img_path
                        , auto: true
                        , acceptMime: 'image/*'
                        , headers: { authorization: 'Bearer ' + config.getToken() }
                        , done: function (res) {
                            layer.closeAll('loading');
                            layer.msg('上传成功');
                            var imgUrl = res.data.url;
                            // if(imgUrl){
                            //     imgUrl=imgUrl.substring(imgUrl.indexOf('/')+1)
                            // }
                            //上传完毕回调
                            $("#photoUrlDiv").html('<img src="' + imgUrl + '" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="photoUrl" value="' + imgUrl + '">');
                        }
                        , error: function (data) {
                            layer.closeAll('loading');
                        }
                    });

                    if (data != undefined) {
                        var photoUrl = data.photoUrl;
                        if (photoUrl) {
                            $("#photoUrlDiv").html('<img src="' + photoUrl + '" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="photoUrl" value="' + photoUrl + '">');
                        }
                        form.val('warehouseForm', data);
                    }
                    if (data) {
                        document.querySelector('input[name="feeItemCode"]').disabled = true;
                    }

                    // //业务类型
                    // admin.initDataSelect('warehouse/warehouse/admin/warehouseFactoryType/list', { limit: 9999 }, "businessType", data ? data.businessType : null, "业务类型", null, null);

                    // const businessTypeList = [{label:"托管业务",businessType:1},{typeName:"加工业务",businessType:2}];












                    form.val('warehouseForm', data);
                    // 表单提交事件
                    form.on('submit(warehouseFormSubmit)', function (d) {
                        layer.load(2);
                        var url = 'warehouse/warehouse/feeItems/add';
                        if (data) {
                            url = 'warehouse/warehouse/feeItems/update';
                        }
                        console.log(d)
                        admin.req(url, d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, { icon: 1 });
                                table.reload('warehouseTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, { icon: 2 });
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            var msg = obj.data.status == 1 ? '确定要禁用吗?':'确定要启用吗?';
            layer.confirm(msg, {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                console.log(obj);
                layer.close(i);
                layer.load(2);
                admin.req('warehouse/warehouse/feeItems/deleteFeeItems', { id: obj.data.id, status: obj.data.status == 1 ? 0 : 1 }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, { icon: 1 });
                        // obj.del();
                        table.reload('warehouseTable');
                    } else {
                        layer.msg(res.msg, { icon: 2 });
                    }
                }, 'GET');
            });
        }

        // 导出excel
        $('#warehouseBtnExport').click(function () {
            var checkRows = table.checkStatus('warehouseTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', { icon: 2 });
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>